import
{
  exportGrossFile,
  exportGrossEndFile,
  importBlackList,
  confirmBlackList,
  downloadBlackListTemplate,
  exportBlackList,
  exportErrorData,
  listErrorData
} from '@/api/grossProfitApi';
import { judgeResponseType } from '@/utils/tools';
import { saveAs } from 'file-saver';
export default {
  data() {
    return {
      importFileDta: {
        visible: false
      },
      showDownDialog: false,
      exportingTop: false,
      exportingBottom: false,
      longPollKey: null
    };
  },
  methods: {
    // 下载黑名单异常数据
    downLoadErrorFileData() {
      exportErrorData().then(res => {
        judgeResponseType(res).then(() => {
          // 说明是正常的文件流
          saveAs(res, '黑名单错误数据.xlsx');
        }).catch(error => {
          this.$message.error(error.resultMessage || '请求异常');
        });
      });
    },
    // 下载已有黑名单
    downloadExistBlackList() {
      exportBlackList().then(res => {
        judgeResponseType(res).then(() => {
          // 说明是正常的文件流
          saveAs(res, '现有黑名单.xlsx');
        }).catch(error => {
          this.$message.error(error.resultMessage || '请求异常');
        });
      });
    },
    // 获取导入不合格的商品列表 分页
    getErrorPage({ current, size }) {
      listErrorData({ current, size }).then(res => {
        this.$refs.importFileRef.setErrorTableData(
          current,
          res.data
        );
      });
    },
    // 下载黑名单模板
    downLoadTemplateFile() {
      downloadBlackListTemplate().then(res => {
        judgeResponseType(res).then(() => {
          // 说明是正常的文件流
          saveAs(res, '黑名单模板.xlsx');
        }).catch(error => {
          this.$message.error(error.resultMessage || '请求异常');
        });
      });
    },
    // 重新上传 或者 确认导入
    isImport(proceed) {
      if (!proceed) {
        // 重新上传
        this.$refs.importFileRef.resetData();
        this.$refs.importFileRef.resetStatus();
        return false;
      }
      // 确认导入
      confirmBlackList({ proceed }).then(res => {
        this.$refs.importFileRef.toThird(res.data || 0);
      }).catch(() => {
        this.$refs.importFileRef.resetData();
      }).finally(() => {
        this.$refs.importFileRef.resetStatus();
      });
    },
    // 导入文件后台解析 点击下一步触发
    firstImport(formData) {
      importBlackList(formData, this.$refs.importFileRef.onUploadProgress).then(res => {
        this.$refs.importFileRef.toSecond({
          errorNum: res.data.errorNum,
          correctNum: res.data.correctNum
        });
        if (res.data.errorNum > 0) {
          // 获取错误数据列表
          this.getErrorPage({
            current: 1,
            size: 10
          });
        }
      }).catch(() => {
        this.$refs.importFileRef.resetProgress();
      });
    },
    // 导入黑名单弹窗关闭
    hanldImportFileColse() {
      this.importFileDta.visible = false;
    },
    // 导入黑名单弹窗显示
    uploadBlackFileTrigger() {
      this.importFileDta.visible = true;
    },
    handleClose() {
      this.showDownDialog = false;
    },
    // 导出报表或者底表
    onExportGrossFile(result, exportType, fileName, searchData, pageType) {
      const params = {
        ...this.initSearchNames(pageType),
        ...searchData,
        exportFormat: result.type,
        exportType // 1 报表 2 底表
      };
      if (exportType === 1) {
        this.exportingTop = true;
        this.getGrossTopFiles(params, fileName, result);
      } else {
        this.exportingBottom = true;
        this.getGrossEndFiles(params);
      }
    },
    // 导出报表
    getGrossTopFiles(params, fileName, result) {
      exportGrossFile(params).then(res => {
        judgeResponseType(res).then(() => {
          // 说明是正常的文件流
          saveAs(res, result.type === 1 ? fileName + '.xlsx' : fileName + '.csv');
        }).catch(error => {
          this.$message.error(error.resultMessage || '请求异常');
        });
        // const blob = new Blob([res], { type: result.type === 1 ? 'application/vnd.ms-excel' : 'text/csv;' });
        // // exportFile(res, result.type === 1 ? fileName + '.xls' : fileName + '.csv', result.type === 1 ? 'application/vnd.ms-excel' : 'text/csv;');
        // saveAs(blob, result.type === 1 ? fileName + '.xlsx' : fileName + '.csv');
      }).finally(() => {
        this.exportingTop = false;
      });
    },
    // 导出底表触发
    getGrossEndFiles(params) {
      exportGrossEndFile(params).then(res => {
        if (res.resultCode === '0000') {
          this.showDownDialog = true;
        }
      }).finally(() => {
        this.exportingBottom = false;
      });
    },
    initSearchNames (pageType) {
      const names = {};
      const searchForm = this.$refs['search-form'];
      const formData = searchForm.formData;
      //  businessOptions 业务态选项
      names.businessCategoriesNames = this.getNames(formData, searchForm.businessOptions, 'businessCategories');
      //  regionsOPtions 大区选项
      names.regionsNames = this.getNames(formData, searchForm.regionsOPtions, 'regions');
      if (pageType !== 'city') {
        //  citiesOptions 城市选项
        names.citiesNames = this.getNames(formData, searchForm.citiesOptions, 'cities');
      }
      if (pageType !== 'city' && pageType !== 'store') {
        //  storesOptions 店铺选项
        names.storesNames = this.getNames(formData, searchForm.storesOptions, 'stores');
      }
      //  goodsCategoriesOne 部门选项
      names.goodsCategoriesOneNames = this.getNames(formData, searchForm.goodsCategoriesOne, 'goodsCategoriesOne');
      //  goodsCategoriesTwo 大类选项
      names.goodsCategoriesTwoNames = this.getNames(formData, searchForm.goodsCategoriesTwo, 'goodsCategoriesTwo');
      //  goodsCategoriesThree 中类选项
      names.goodsCategoriesThreeNames = this.getNames(formData, searchForm.goodsCategoriesThree, 'goodsCategoriesThree');
      //  goodsCategoriesFour 小类选项
      names.goodsCategoriesFourNames = this.getNames(formData, searchForm.goodsCategoriesFour, 'goodsCategoriesFour');
      return names;
    },
    getNames(formData, options, label) {
      if (!formData[label]) {
        return [];
      }
      return options.filter(v => formData[label].includes(v.value)).map(v => v.name);
    }
  }
};
